<?php
namespace Ucmaim\Controller\Admin;

class ConfigController extends CommonController{

    public function _initialize(){
        parent::_initialize();
    }

    //时间
    public function dealtime(){
        $config = M('config');
        if( IS_POST ){
            $ship = I('post.shipments_time');
            $rece = I('post.receipt_time');
            $pays = I('post.pay_time');
            $sxf  = I('post.poundage');
            if($ship<=0 || !is_numeric($ship)){
                $this->error('请正确输入发货时间');
            }
            if($rece<=0 || !is_numeric($rece)){
                $this->error('请正确输入收货时间');
            }
            if($pays<=0 || !is_numeric($pays)){
                $this->error('请正确输入支付时间');
            }
            if($sxf<0 || !is_numeric($sxf)){
                $this->error('请正确输入手续费');
            }
            $pay = $pays*3600;
            $shipments = $ship*3600;
            $receipt = $rece*3600;
            $deliverytime['value'] = $shipments;
            $receivingtime['value'] = $receipt;
            $paytime['value'] = $pay;
            $poundage['value'] = $sxf;

            $config->where("`key`='SEND_EXPIRE'")->save($deliverytime);
            $config->where("`key`='CONFIRM_EXPIRE'")->save($receivingtime);
            $config->where("`key`='PAY_EXPIRE'")->save($paytime);
            $config->where("`key`='POUNDAGE'")->save($poundage);
            $this->success('修改成功');
        }else{
            $this->delivery=$config->where("`key`='SEND_EXPIRE'")->find();
            $this->receiving=$config->where("`key`='CONFIRM_EXPIRE'")->find();
            $this->pay=$config->where("`key`='PAY_EXPIRE'")->find();
            $this->poundage=$config->where("`key`='POUNDAGE'")->find();
            $this->display();
        }

    }

    //幻灯片
    public function Pic(){
        $do=I('get.do');
        switch($do){
            case 'add':
                $this->_picAdd();
                break;
            case 'edit':
                $this->_picEdit();
                break;
            case 'del':
                $this->_picDel();
                break;
            default:
                $ad=M('ad');
                $this->vlist=$ad->order('ad_id desc')->select();
                $this->display('PicList');
        }
    }
    private function _picAdd(){
        if( IS_GET ){
            $this->display('PicAdd');
        }else{
            $ad=M('ad');
            $rules = array(
                array('ad_title','require','必须填写标题！'),
                array('ad_type','require','必须填写栏目！'),
            );
            if (!$ad->validate($rules)->create()){
                exit($ad->getError());
            }
            if($_FILES["photo"]["name"]){
                $config = array(
                    'maxSize'    =>    3145728,
                    'rootPath'   =>    './Application/Ucmaim/Uploads/',
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                    'subName'    =>    array('date','Ymd'),
                );
                $upload = new \Think\Upload($config);
                $info   =   $upload->upload();
                foreach($info as $v){
                    $pitcure = $v['savepath'].$v['savename'];
                }
            }else{
                $this->error('请上传图片');
            }
            $data['ad_type']=I('post.ad_type');
            $data['ad_title']=I('post.ad_title');
            $data['ad_pic']=$pitcure;
            $data['ad_sort']=I('post.pic_sort');
            $data['ad_url']=I('post.pic_link');
            $mark=$ad->data($data)->add();
            if($mark){
                $this->redirect('Admin/Config/Pic');
            }else{
                $this->error('添加失败');
            }

        }

    }

    private function _picEdit(){
        $ad=M('ad');
        if(IS_GET){
            $id=I('get.id');
            $this->info=$ad->where("ad_id=$id")->find();
            $this->display('PicAdd');
        }else{
            $rules = array(
                array('ad_title','require','必须填写标题！'),
                array('ad_type','require','必须填写栏目！'),
            );
            if (!$ad->validate($rules)->create()){
                exit($ad->getError());
            }
            $data['ad_type']=I('post.ad_type');
            $data['ad_title']=I('post.ad_title');
            $data['ad_sort']=I('post.pic_sort');
            $data['ad_url']=I('post.pic_link');
            $id=I('post.pic_id');
            if($_FILES["photo"]["name"]){
                $config = array(
                    'maxSize'    =>    3145728,
                    'rootPath'   =>    './Application/Ucmaim/Uploads/',
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                    'subName'    =>    array('date','Ymd'),
                );
                $upload = new \Think\Upload($config);
                $info   =   $upload->upload();
                foreach($info as $v){
                    $pitcure = $v['savepath'].$v['savename'];
                }
                $data['ad_pic']=$pitcure;
            }
            $mark=$ad->where("ad_id=$id")->save($data);
            if($mark){
                $this->redirect('Admin/Config/Pic');
            }else{
                $this->error('修改失败');
            }
        }

    }

    private function _picDel(){
        $ad=M('ad');
        $id=I('get.id');
        $mark=$ad->where("ad_id=$id")->delete();
        if($mark){
            $this->redirect('Admin/Config/Pic');
        }else{
            $this->error('删除失败');
        }
    }
}